/**
 * 
 */
package com.geekteam.masterenglish.biz;

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.net.Uri;
import android.os.RemoteException;

import com.geekteam.masterenglish.entity.Album;
import com.geekteam.masterenglish.entity.Lession;
import com.geekteam.masterenglish.provider.EEContract;
import com.geekteam.masterenglish.utils.Lists;
import com.geekteam.masterenglish.utils.Utils;

/**
 * @author MTROL
 *
 */
public class LessionBiz {
	public static void processLession(Context context, Album album){
		final ContentResolver resolver = context.getContentResolver();
		ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>();
		batch = getLessionContent(album);
		
		try {
			resolver.applyBatch(EEContract.CONTENT_AUTHORITY, batch);
		} catch (RemoteException e) {
			throw new RuntimeException("Problem applying batch operation", e);
		} catch (OperationApplicationException e) {
			throw new RuntimeException("Problem applying batch operation", e);
		}
		
		context.getContentResolver().notifyChange(EEContract.Lession.CONTENT_URI, null);
	}
	
	public static ArrayList<ContentProviderOperation> getLessionContent(Album album){
		final ArrayList<ContentProviderOperation> batch = Lists.newArrayList();
		album.mInfo.mBookId = Utils.getBookId(album.mInfo.mId);
		AlbumBiz.parseAlbumContent(album.mInfo, batch);
		if(album.mLessions!=null){
			for(int i = 0; i<album.mLessions.size(); i++){
				Lession lession = album.mLessions.get(i);
				lession.mAlbumId = album.mInfo.mId;
				lession.mAlbumName = album.mInfo.mTitle;
				parseLessionContent(lession, batch);
			}
		}
		
		return batch;
	}
	
	public static void parseLessionContent(Lession lession, ArrayList<ContentProviderOperation> batch){
		ContentProviderOperation.Builder builder = ContentProviderOperation
				.newInsert(EEContract.addCallerIsSyncAdapterParameter(EEContract.Lession.CONTENT_URI));
		
		builder.withValue(EEContract.Lession.ID, lession.mId);
		builder.withValue(EEContract.Lession.TITLE, lession.mTitle);
		builder.withValue(EEContract.Lession.COVER, lession.mCover);
		builder.withValue(EEContract.Lession.DESCRIPTION, lession.mDescription);
		builder.withValue(EEContract.Lession.ISFAVORITE, lession.isFavorite?1:0);
		builder.withValue(EEContract.Lession.LINK_128, lession.mLinkPlay128);
		builder.withValue(EEContract.Lession.LINK_320, lession.mLinkPlay320);
		builder.withValue(EEContract.Lession.LINKDOWN_128, lession.mLinkDown128);
		builder.withValue(EEContract.Lession.LINKDOWN_320, lession.mLinkDown320);
		builder.withValue(EEContract.Lession.TEXT, lession.mText);
		builder.withValue(EEContract.Lession.TIME_LENGTH, lession.mTimeLength);
		builder.withValue(EEContract.Lession.TOTAL_LISTEN, lession.mTotalListen);
		builder.withValue(EEContract.Lession.ALBUM_ID, lession.mAlbumId);
		builder.withValue(EEContract.Lession.ALBUM_NAME, lession.mAlbumName);
		if(lession.mCurrentTime!=-1){
			lession.mCurrentTime = System.currentTimeMillis();
		}
		builder.withValue(EEContract.Lession.UPDATED, lession.mCurrentTime);
		
		batch.add(builder.build());
	}
	
	public static void updateLession(Context context, Lession entity){
		final ContentResolver resolver = context.getContentResolver();
		Uri uri = EEContract.Lession.buildLessionId(entity.mId);
		ContentValues values = new ContentValues();
		values.put(EEContract.Lession.ID, entity.mId);
		values.put(EEContract.Lession.TITLE, entity.mTitle);
		values.put(EEContract.Lession.TEXT, entity.mText);
		values.put(EEContract.Lession.LINKDOWN_128, entity.mLinkDown128);
		values.put(EEContract.Lession.LINKDOWN_320, entity.mLinkDown320);
		values.put(EEContract.Lession.LINK_128, entity.mLinkPlay128);
		values.put(EEContract.Lession.LINK_320, entity.mLinkPlay320);
		values.put(EEContract.Lession.TOTAL_LISTEN, entity.mTotalListen);
		long currentTime = System.currentTimeMillis();
		values.put(EEContract.Lession.UPDATED, currentTime);
		
		resolver.update(uri, values, null, null);
		resolver.notifyChange(uri, null);
	}
}
